package com.wuchao.util;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*@Component*/
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        //打印请求的地址
        String requestURI = req.getRequestURI();
        System.out.println(requestURI);
        if (!requestURI.equals("/")
                && !requestURI.equals("/login")
                && !requestURI.equals("/doLogin")
                && !requestURI.equals("/logout")
                && !requestURI.equals("/css/*")
                && !requestURI.equals("/login.jsp")
                && !requestURI.equals("/js/*")) {
            if (req.getSession().getAttribute("user") == null
                    || req.getSession().getAttribute("user") == "") {
                System.out.println("没有登录");
                res.sendRedirect("login.jsp");
            }
        }
        //直接放行
        chain.doFilter(request,response);
    }

    @Override
    public void destroy() {

    }
}
